﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Enterprise.DataCMS.WebExplorer.Controls
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class TreeDataManager : IHttpHandler
    {
        private const string SUCCESS = "1";
        private const string FAILED = "-1";
        private const string FAILED_FILE_WITH_SAME_NAME_EXIST = "-2";
        private HttpContext con;
        private int level = 0;
        private string treedata = string.Empty;
        private SiteMapNode Parent;
        private string TreeDataManagerPath = "/WebSiteExplorer/Controls/TreeDataManager.ashx";


        public void ProcessRequest(HttpContext context)
        {

            this.con = context;
            string action = context.Request["action"];
            string elementId = context.Request["elementId"];
            string ownerEl = context.Request["ownerEl"];
            string name = context.Request["name"];
            string destOwnerEl = context.Request["destOwnerEl"];

            switch (action)
            {
                case "insertElement":
                    context.Response.Write(InsertElement(name, ownerEl));
                    break;

                case "getElementList":
                    context.Response.Write(CreateTree(ownerEl));
                    break;

                case "updateElementName":
                    //context.Response.Write(UpdateName(name, elementId));
                    break;

                case "deleteElement":
                    //context.Response.Write(DeleteElement(elementId));
                    break;

                case "changeOrder":
                    //context.Response.Write(ChangeOrder(elementId, destOwnerEl));
                    break;
            }
        }

        private string InsertElement(string title, string ownerEl)
        {
            try{
                string sql = "INSERT INTO SiteMap VALUES(id,Title,Description,Url,Roles,Parent,SortOrder,Active,Hidden,PublishDate,UnPublishDate,UsePublishDate,SEODescription,SEOKeywords,IsStartPage,PortalID)";



                return SUCCESS;
            }
            catch
            {
                return FAILED;
            }
        }



        /// <summary>
        /// Get the parent node from NodeId 
        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
        private string CreateTree(string NodeId)
        {
            foreach (SiteMapNode node in SiteMap.RootNode.GetAllNodes())
                if (node.Key != null && (node.Key == NodeId.Trim()))
                {
                    Parent = node;
                    return CreateTree(node, 0);
                }
            return "";
        }



        /// <summary>
        /// Creates the tree to return
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        private string CreateTree(SiteMapNode nodes, int level)
        {
            treedata = string.Empty;

            foreach (SiteMapNode node in nodes.ChildNodes)
            {
                if (node.Key != null && node.IsDescendantOf(Parent))
                {
                    treedata += CreateNode(node.Key, node.Title, node.HasChildNodes, node.ParentNode.Key);
                    treedata += "</li>";
                }
            }
            return treedata;
        }

        /// <summary>
        /// Creates a node to return
        /// </summary>
        /// <param name="id"></param>
        /// <param name="text"></param>
        /// <param name="hasChildNodes"></param>
        /// <param name="parentid"></param>
        /// <returns></returns>
        private string CreateNode(string id, string text, bool hasChildNodes, string parentid)
        {
            string s = string.Empty;

            if (hasChildNodes)
            {
                s = "\n<li class=\"text\" id=\"" + id + "\">\n";
                s += "  <span>" + text + "</span>\n";
                s += "   <ul class=\"ajax\">\n";
                s += "      \t<li id=\"" + id + "\">{url:"+TreeDataManagerPath+"?action=getElementList&ownerEl=" + parentid + "}</li>\n";
                s += "   </ul>\n";

            }
            else
            {
                s = "<li class='text' id=\"" + id + "\"><span>" + text + "</span>";
            }
            return s;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
